package org.jackysoft.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.data.AbstractDataProvider;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.User;
import org.jackysoft.mapper.DepartmentMapper;
import org.jackysoft.query.CompareRelation;
import org.jackysoft.query.RunnerQuery;
import org.jackysoft.service.DepartmentService;
import org.jackysoft.service.UserService;
import org.jackysoft.util.UserAllowedPredicate;
import org.jackysoft.ztree.DepartmentZtreeFunction;
import org.jackysoft.ztree.UserZtreeFunction;
import org.jackysoft.ztree.ZTreeNode;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
public class DepartmentServiceImpl extends
		AbstractDataProvider<Department> implements
		DepartmentService {

	static final Log logger = LogFactory.getLog(DepartmentServiceImpl.class);
	
	
	@Resource
	private UserService userService;

	@Resource
	private DepartmentMapper mapper;
	
	private static final Map<String, String> alias = Maps.newHashMap();

	public DepartmentServiceImpl() {
		super();
		alias.put("一分厂", "一分厂");
		alias.put("三分厂", "三分厂");
		alias.put("五分厂", "五分厂");
		alias.put("冲压件厂", "冲压件厂");
		alias.put("冲压", "冲压件厂");
		alias.put("机械加工厂", "机械加工厂");
		alias.put("机加", "机械加工厂");
		alias.put("化工分厂", "化工分厂");
		alias.put("化工", "化工分厂");
		alias.put("庆华火工品研发分中心", "庆华火工品研发分中心");
		alias.put("火工品研发分中心", "庆华火工品研发分中心");
		alias.put("火工所", "庆华火工品研发分中心");
		alias.put("军用热电池研发中心", "军用热电池研发中心");
		alias.put("军用热电池中心", "军用热电池研发中心");
		alias.put("电池所", "军用热电池研发中心");
		alias.put("采购中心", "采购中心");
		alias.put("采购部", "采购中心");
		alias.put("采购", "采购中心");
		alias.put("塑业公司", "塑业公司");
		alias.put("塑料", "塑业公司");
		alias.put("塑业", "塑业公司");
	}

	/**
	 * 删除部门及其相关部门
	 * */
	@Override
	public void remove(Department t) throws DataAccessException {	
		mapper.remove(t.getId());		
	}



	@Override
	public Collection<Department> getChildren(String parentId)
			throws DataAccessException {
		
		
		String pid = parentId;
		if(StringUtils.isBlank(parentId) || "root".equals(parentId)){
	        pid=null;     		
		}
		Collection<Department> list = mapper.getChildren(pid);
		return list;
	
	}
	

	@Override
	public Collection<Department> getChildren(int dtype)
			throws DataAccessException {		
		
		return mapper.getChildrenByType(dtype);
		
	}

	@Override
	public Collection<ZTreeNode> ztree(String parenId)
			throws DataAccessException {
		
		
		Collection<Department> depts = Lists.newArrayList();
		Collection<ZTreeNode> jsNodes = Lists.newArrayList();
		if (parenId == null) {
			parenId="root";
		}

		if ("root".equalsIgnoreCase(parenId)) {
			jsNodes = (Collections2.transform(getChildren(""),
					new DepartmentZtreeFunction()));
			return jsNodes;
		}
	
		Collection<User> users = userService.findByDepartment(parenId);
		users = Collections2.filter(users, new UserAllowedPredicate());
		depts = getChildren(parenId);

		Collection<ZTreeNode> jsNode = Collections2.transform(depts,
				new DepartmentZtreeFunction());
		jsNodes.addAll(jsNode);
		jsNode = Collections2.transform(users, new UserZtreeFunction());
		jsNodes.addAll(jsNode);
		return jsNodes;
	}

	@Override
	public Collection<ZTreeNode> ztree(Department bean,
			Collection<Integer> dtypes) throws DataAccessException {
		Collection<ZTreeNode> nodes = Lists.newArrayList();
		nodes.addAll(Collections2.transform(getChildren(bean.getId()),
				new DepartmentZtreeFunction()));
		return nodes;
	}

	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public Department getDepartment(String name) throws DataAccessException {
		String tname = alias.get(name);
		if (tname == null)return null;
		RunnerQuery query = new RunnerQuery();
		query.addField("name", tname);
		
		Department t = mapper.queryOne(query);;
		return t;
	}

	@Override
	public Collection<Department> fromWorkflowSetting(String value) {
		
		RunnerQuery query = new RunnerQuery();
		query.addField("id", CompareRelation.类似于, value);	
		List<Department> list = mapper.query(query);
		return list;
	}

	@Override
	public Department findByUser(String user) {
		Department child = mapper.findByUser(user);
		String oid = child.getId();
		List<String> stack = new ArrayList<>();
		stack.add(child.getName());
		while(null!=child.getParent()){
			child = mapper.findOne(child.getParent());
		    stack.add(child.getName());
		}
		Department last = new Department();
		last.setId(oid);
		String name = "";
		int len = stack.size();
		for(int i=len-1;len>=0;i--){
			name+=stack.get(i)+" ";
		}
		last.setName(name);
		
		return last;
	}

}
